package com.sgb.kjwl.viewmodel.transport;

import androidx.databinding.ObservableField;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.transport.CarsSourcesEntiity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceFilterEntity;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.TransportRepertory;
import com.sgb.kjwl.view.ui.activity.transport.CarsInfoActivity3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarsInfoModel3 extends BaseViewModel {

    private CarsInfoActivity3 activity;
    private TransportRepertory repertory;
    public ObservableField<String> messageNum = new ObservableField<>("");
    public ObservableField<GoodsSourceFilterEntity> filterEntity = new ObservableField<>(new GoodsSourceFilterEntity());
    public ObservableField<GoodsSourceLocationEntity> sendLocation = new ObservableField<>(new GoodsSourceLocationEntity());
    public ObservableField<GoodsSourceLocationEntity> purposeLocation = new ObservableField<>(new GoodsSourceLocationEntity());
    public ObservableField<GoodsInfoTypeEntity3> sourceType = new ObservableField<>(new GoodsInfoTypeEntity3());

    private int pageSize = 10;

    public CarsInfoModel3(CarsInfoActivity3 activity3, TransportRepertory repertory){
        this.activity = activity3;
        this.repertory = repertory;
    }

    //车辆信息
    public void getCarsInfoList(int pageNum){

        Map map = new HashMap();
        map.put("areaCode", repertory.getAreaCode());
        map.put("page", pageNum);
        map.put("limit", pageSize);
        if(null!=filterEntity.get()){
            if(null==filterEntity.get().getCarType()){
                map.put("carTypeNos", "");
            }else{
                List lt = new ArrayList();
                for(GoodsInfoTypeEntity3 entity : filterEntity.get().getCarType()){
                    lt.add(entity.getNo());
                }
                map.put("carTypeNos", lt);
            }

            if(null==filterEntity.get().getLengthType()){
                map.put("carLenNos", "");
            }else{
                List lt = new ArrayList();
                for(GoodsInfoTypeEntity3 entity : filterEntity.get().getLengthType()){
                    lt.add(entity.getNo());
                }
                map.put("carLenNos", lt);
            }
        }

        if(null!=sendLocation.get()){
            if(TextUtils.isEmpty(sendLocation.get().getProvinceNo())){
                map.put("provinceNo","");
            }else{
                map.put("provinceNo",sendLocation.get().getProvinceNo());
            }
        }

        if(null!=sendLocation.get()){
            if(TextUtils.isEmpty(sendLocation.get().getCityNo())){
                map.put("cityNo","");
            }else{
                map.put("cityNo",sendLocation.get().getCityNo());
            }
        }

        if(null!=sendLocation.get()){
            if(TextUtils.isEmpty(sendLocation.get().getAreaNo())){
                map.put("districtNo","");
            }else{
                map.put("districtNo",sendLocation.get().getAreaNo());
            }
        }

        if(null!=purposeLocation.get()){
            if(TextUtils.isEmpty(purposeLocation.get().getProvinceNo())){
                map.put("destProvinceNo","");
            }else{
                map.put("destProvinceNo",purposeLocation.get().getProvinceNo());
            }
        }

        if(null!=purposeLocation.get()){
            if(TextUtils.isEmpty(purposeLocation.get().getCityNo())){
                map.put("destCityNo","");
            }else{
                map.put("destCityNo",purposeLocation.get().getCityNo());
            }
        }

        if(null!=purposeLocation.get()){
            if(TextUtils.isEmpty(purposeLocation.get().getAreaNo())){
                map.put("destDistrictNo","");
            }else{
                map.put("destDistrictNo",purposeLocation.get().getAreaNo());
            }
        }

        Log.v("sourceList_map",map.toString());
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarFilterSourceList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<CarsSourcesEntiity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<CarsSourcesEntiity>>>> call, Response<BaseEntity<ProListEntity<List<CarsSourcesEntiity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null){
                        MToast.showToast(activity, "暂无数据");
                        return;
                    }
                    if (response.body().getData().getList() == null){
                        MToast.showToast(activity, "暂无数据");
                        return;
                    }
                    Log.v("sourceList",response.body().getData().getList().toString());
                    activity.getCarsInfoData(response.body().getData().getList());
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<CarsSourcesEntiity>>>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });

    }

    public void getGoodsTypeData(){

        String carsTypeString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_TYPE,"");
        String carsLengthString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_LENGTH,"");
        List<GoodsInfoTypeEntity3> carsType = new Gson().fromJson(carsTypeString, new TypeToken<List<GoodsInfoTypeEntity3>>(){}.getType());
        List<GoodsInfoTypeEntity3> carsLengthType = new Gson().fromJson(carsLengthString, new TypeToken<List<GoodsInfoTypeEntity3>>(){}.getType());
        activity.getCarsTypeData(carsType);
        activity.getCarsLengthData(carsLengthType);

    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //货车类型
    public void getCarsType(){
        APIManager.getInstance().getDriverAPI().getCarType("1").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    activity.getCarsTypeData(response.body().getData());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //货车长度
    public void getCarsLength(){
        APIManager.getInstance().getDriverAPI().getCarLenType("1").enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    activity.getCarsLengthData(response.body().getData());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_INFO_LENGTH,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //运输方式
    public void getTransType(){
        APIManager.getInstance().getDriverAPI().getTransType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.TRANSPORT_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //装载方式
    public void getHandleType(){
        APIManager.getInstance().getDriverAPI().getHandleType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CARS_HANDLING_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //付款方式
    public void getPayType(){
        APIManager.getInstance().getDriverAPI().getPayType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PAY_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    //发票方式
    public void getInvoiceType(){
        APIManager.getInstance().getDriverAPI().getInvoiceType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.INVOICE_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }
}
